package com.huika.dealontop.control.base.extend;

import android.content.Context;
import android.view.View;

import com.huika.dealontop.R;
import com.huika.dealontop.utils.UIUtils;

public class LoadMoreHolder extends BaseViewHolder<Integer> implements View.OnClickListener {
    public static final int STATE_LOADING = 0;
    public static final int STATE_ERROR = 1;
    public static final int STATE_EMPTY = 2;
    public static final int STATE_DISPLAY = 3;

    private View mErrorView;
    private View mLoadingView;
    private View mNoMoreView;

    private int mCurrentSate;

    public OnLoadMoreOnClickListener getMoreOnClickListener() {
        return moreOnClickListener;
    }

    public void setMoreOnClickListener(OnLoadMoreOnClickListener moreOnClickListener) {
        this.moreOnClickListener = moreOnClickListener;
    }

    private OnLoadMoreOnClickListener moreOnClickListener;

    public interface  OnLoadMoreOnClickListener{
        void onLoadMoreClick(View view);
    }
    public LoadMoreHolder(Context context) {
        super(context);
    }

    @Override
    protected View initView() {
        View view = View.inflate(UIUtils.getContext(), R.layout.item_load_more, null);

        view.setOnClickListener(this);

        return view;
    }

    @Override
    protected void assignView(View view) {
        mErrorView = view.findViewById(R.id.item_loadMore_container_retry);
        mLoadingView = view.findViewById(R.id.item_loadMore_container_loading);
        mNoMoreView = view.findViewById(R.id.item_loadMore_no_more);
    }

    @Override
    public void refreshView(Integer data) {
        this.mCurrentSate = data;
        mRootView.setVisibility(STATE_DISPLAY == mCurrentSate ? View.GONE : View.VISIBLE);

        switch (data) {
            case STATE_EMPTY:
                mErrorView.setVisibility(View.GONE);
                mLoadingView.setVisibility(View.GONE);
                mNoMoreView.setVisibility(View.VISIBLE);
                break;
            case STATE_ERROR:
                mErrorView.setVisibility(View.VISIBLE);
                mLoadingView.setVisibility(View.GONE);
                mNoMoreView.setVisibility(View.GONE);
                break;
            case STATE_LOADING:
                mLoadingView.setVisibility(View.VISIBLE);
                mErrorView.setVisibility(View.GONE);
                mNoMoreView.setVisibility(View.GONE);
                break;
            default:
                break;
        }
    }

    public int getCurrentState() {
        return mCurrentSate;
    }

    @Override
    public void onClick(View view) {
        if(mCurrentSate == STATE_ERROR){
            if(moreOnClickListener != null){
                moreOnClickListener.onLoadMoreClick(view);
            }
        }
    }
}
